package com.example.delegues.adapter;

import java.util.ArrayList;

import com.example.delegues.model.User;
import com.example.deleguesapp.R;
import com.example.deleguesapp.manager.DeleguesManager;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EleveAdapter extends BaseAdapter {
	private ArrayList<User> listEleves;
	private boolean listEmpty = true;
	private LayoutInflater inflater;
	private Activity activity; 
	
	public void loadData(){
//		listEleves = new ArrayList<User>();
//		listEleves.add(new User((long)1,"NOCK", "Vincent", null, null, new Float(0), null,null,new Integer(0)));
//		listEleves.add(new User((long)2,"ROBERT", "Gregoire", null, null, new Float(0), null,null,new Integer(0)));
//		listEleves.add(new User((long)3,"NINI", "TocToc", null, null, new Float(0), null,null,new Integer(0)));
//		listEmpty = false;
//		for (User user : listEleves) {
//			DeleguesManager.getInstance(activity).saveUser(user);
//		}
		
		listEleves = DeleguesManager.getInstance(activity).getListUser();
		listEmpty = false;
		notifyDataSetChanged();
	}
	
	public EleveAdapter(Activity context) {
		// TODO Auto-generated constructor stub
		this.activity = context;
		this.inflater = LayoutInflater.from(activity);
		this.loadData();
	}

	public int getCount() {
		if (listEmpty)
			return 0;
		else
			return listEleves.size();
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return listEleves.get(arg0);

	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		User eleve = listEleves.get(position);

		if (convertView == null) {
			convertView = (LinearLayout) inflater.inflate(
					R.layout.list_eleves_item, null);
			viewHolder = new ViewHolder();
			viewHolder.textView = (TextView) convertView
					.findViewById(R.id.textView1);
			viewHolder.image = (ImageView) convertView
					.findViewById(R.id.imageView1);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
			
		}
		if (viewHolder != null){
			viewHolder.textView.setText(eleve.getForname()+" "+eleve.getName());
			
		}
		return convertView;
	}

	private class ViewHolder {
		TextView textView;
		ImageView image;
	}
}
